home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / gnu_oleo_1_2_2.lha / oleo-1.2.2 / list.c < prev    next >
C/C++ Source or Header  |  1993-03-03  |  4KB  |  190 lines

  1. /*    Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This file is part of Oleo, the GNU Spreadsheet.
  4.  
  5. Oleo is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. Oleo is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with Oleo; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "funcdef.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include "sysdef.h"
  23. #include "global.h"
  24. #include "cell.h"
  25. #include "io-generic.h"
  26. #include "io-abstract.h"
  27. #include "regions.h"
  28. #include "io-utils.h"
  29.  
  30.  
  31.  
  32.  
  33.  
  34. static char sl_sep = '\t';
  35.  
  36.  
  37.  
  38. /* This file reads/writes files containing values in separated lists,
  39.    sl_sep is the separating character.  This isn't really a save-file
  40.    format, but it is useful for reading and writing tables written by other
  41.    programs.
  42.  
  43.    Note that this format loses *most* of the information about the cells,
  44.    including formuale, formats, column widths, etc
  45.  */
  46. void
  47. list_read_file (fp, ismerge)
  48.      FILE *fp;
  49.      int ismerge;
  50. {
  51.   char cbuf[1024];
  52.   CELLREF row, col;
  53.   char *bptr;
  54.   char *eptr;
  55.   char *ptr;
  56.   char tchar;
  57.   int endit;
  58.   unsigned lineno;
  59.   int string;
  60.  
  61.   lineno = 0;
  62.   if (!ismerge)
  63.     clear_spreadsheet ();
  64.   row = curow;
  65.   col = cucol;
  66.  
  67.   while (fgets (&cbuf[1], sizeof (cbuf) - 3, fp))
  68.     {
  69.       lineno++;
  70.       if (lineno % 50 == 0)
  71.     io_info_msg ("Line %d", lineno);
  72.       endit = 0;
  73.       for (bptr = &cbuf[1];; bptr = eptr + 1)
  74.     {
  75.       eptr = (char *)index (bptr, sl_sep);
  76.       if (!eptr)
  77.         {
  78.           eptr = (char *)index (bptr, '\n');
  79.           endit++;
  80.         }
  81.       string = 0;
  82.       for (ptr = bptr; ptr != eptr; ptr++)
  83.         if (!isdigit (*ptr) && *ptr != '.' && *ptr != 'e' && *ptr != 'E')
  84.           {
  85.         string++;
  86.         break;
  87.           }
  88.       if (string)
  89.         {
  90.           bptr[-1] = '"';
  91.           eptr[0] = '"';
  92.           tchar = eptr[1];
  93.           eptr[1] = '\0';
  94.           new_value (row, col, &bptr[-1]);
  95.           eptr[1] = tchar;
  96.         }
  97.       else
  98.         {
  99.           eptr[0] = '\0';
  100.           new_value (row, col, bptr);
  101.         }
  102.       if (endit)
  103.         break;
  104.       col++;
  105.     }
  106.       row++;
  107.       col = cucol;
  108.     }
  109. }
  110.  
  111. void
  112. list_write_file (fp, rng)
  113.      FILE *fp;
  114.      struct rng *rng;
  115. {
  116.   CELLREF row, col;
  117.   int repressed;
  118.   CELL *cp;
  119.  
  120.   if (!rng)
  121.     rng = &all_rng;
  122.   for (row = rng->lr;; row++)
  123.     {
  124.       repressed = 0;
  125.       for (col = rng->lc;; col++)
  126.     {
  127.       if ((cp = find_cell (row, col)) && GET_TYP (cp))
  128.         {
  129.           while (repressed > 0)
  130.         {
  131.           putc (sl_sep, fp);
  132.           --repressed;
  133.         }
  134.           repressed = 1;
  135.           switch (GET_TYP (cp))
  136.         {
  137.         case TYP_FLT:
  138.           fputs (flt_to_str (cp->cell_flt), fp);
  139.           break;
  140.         case TYP_INT:
  141.           fprintf (fp, "%ld", cp->cell_int);
  142.           break;
  143.         case TYP_STR:
  144.           fputs (cp->cell_str, fp);
  145.           break;
  146.         case TYP_BOL:
  147.           fputs (bname[cp->cell_bol], fp);
  148.           break;
  149.         case TYP_ERR:
  150.           fputs (ename[cp->cell_err], fp);
  151.           break;
  152. #ifdef TEST
  153.         default:
  154.           panic ("Unknown type %d in write_sl_file()", GET_TYP (cp));
  155.           break;
  156. #endif
  157.         }
  158.         }
  159.       else
  160.         repressed++;
  161.       if (col == rng->hc)
  162.         break;
  163.     }
  164.       putc ('\n', fp);
  165.       if (row == rng->hr)
  166.     break;
  167.     }
  168. }
  169.  
  170. int
  171. list_set_options (set_opt, option)
  172.      int set_opt;
  173.      char *option;
  174. {
  175.   if (set_opt && !strincmp (option, "list ", 5))
  176.     {
  177.       option += 5;
  178.       sl_sep = string_to_char (&option);
  179.       return 0;
  180.     }
  181.   return -1;
  182. }
  183.  
  184. void
  185. list_show_options ()
  186. {
  187.   io_text_line ("File format: list    (character separated list of cell values)");
  188.   io_text_line ("Save file element separator: %s", char_to_string (sl_sep));
  189. }
  190.